package com.zrx.app.android.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.zrx.app.android.R;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by liuji on 2017/4/14.
 *
 *
 * ？？？？
 */

public class TextFrag extends BaseFragment {
    private static final String TAG = "VideoFrag";
    TextView label_keyName = null;
    EditText text_content = null;
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        final View _view = inflater.inflate(R.layout.layout_text, container, false);
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        try {

            label_keyName = (TextView) _view.findViewById(R.id.label_keyName);
            text_content = (EditText) _view.findViewById(R.id.text_content);

            label_keyName.setText(key + ":");

            if (node.containsKey("val")) {
                Object _val = node.get("val");
                if(_val instanceof String) {
                    if(key!=null&&key.contains("身份证")){
                        String idCardNum = (String) _val;
                        if(idCardNum.length()>11)
                            idCardNum=idCardNum.substring(0,6)+"********"+idCardNum.substring(14);
                        text_content.setText(idCardNum);
                    }else if(key!=null&&(key.equals("联系电话")||key.equals("手机"))){
                        String tel = (String) _val;
                        if(tel.length()>4)
                            tel=tel.substring(0,tel.length()-4)+"****";
                        text_content.setText(tel);
                    }else
                        text_content.setText(_val.toString());
                }else if(_val instanceof Long){
                    try {
                        text_content.setText(sdf.format(_val));
                    }catch (Exception e){
                        Log.d(TAG,e.getMessage());
                    }
                }

            }
            if (node.containsKey("editable"))
                text_content.setEnabled(node.getBoolean("editable"));

            if (node.containsKey("description"))
                text_content.setHint(node.getString("description"));


            text_content.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if(!node.containsKey("val")){
                        if(key.contains("日期")||key.contains("时间"))
                            node.put("val",new Date());
                        else
                            node.put("val","");
                    }

                    if(node.get("val") instanceof String)
                        node.put("val", s.toString());
                    else if(node.get("val") instanceof Long){
                        try {
                            node.put("val",sdf.parse(s.toString()).getTime());
                        }catch (Exception e){
                            Log.d(TAG,e.getMessage());
                        }
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });

            if (key.contains("链接")) {
                text_content.setEnabled(true);

                text_content.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        Intent intent = new Intent();
                        intent.setAction("android.intent.action.VIEW");
                        Uri content_url = Uri.parse(node.getString("val"));
                        intent.setData(content_url);
                        startActivity(intent);
                        return false;
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return _view;
    }

    public String getKey() {
        return key;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

}
